//
//  HEConsulDetailTipsCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEConsulDetailTipsCell.h"
#import "HEAITipsCell.h"

@interface HEConsulDetailTipsCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
@property (nonatomic, strong) NSArray <HEDoctorInfoModel *> *showList;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *contentLb;
@property (nonatomic, assign) CGFloat height;
@end

@implementation HEConsulDetailTipsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    
    _titleLb = [UILabel new];
    _titleLb.text = @"小贴士：";
    _titleLb.font = DYFont(15);
    _titleLb.textColor = CNavBgColor;
    [self.contentView addSubview: _titleLb];
    
 
 
    _contentLb = [UILabel new];
    _contentLb.text = @"咨询记录：";
    _contentLb.font = DYFont(16);
    _contentLb.textColor = CNavBgColor;
    _contentLb.numberOfLines = 0;
    [self.contentView addSubview: _contentLb];
    
    
    
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.estimatedItemSize = CGSizeMake( (KScreenWidth - 15*4) / 2.0, 50);
 
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = CSplitLineColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self.contentView addSubview:_collectionView];

    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass: HEAITipsCell.class forCellWithReuseIdentifier: KHEAITipsCell];
 
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    //设置子视图与父视图的约束，以便b子视图变化是能“撑”起父视图
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(15);
        make.right.left.equalTo(@0);
        make.height.mas_equalTo(@1).priorityLow();//设置一个高度，以便赋值后更新
    }];
 
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    [_collectionView reloadData];
    [_collectionView layoutIfNeeded];
    
    /*
     collectionView加载完毕后执行更新Layout
     **/
    
    //获取collectionview高度
    _height = [fiterModel getCollectionHeight: _collectionView];

    NSLog(@"height = %lf", _height);
    
    //更新collectionView
    [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
         make.height.mas_equalTo(@(_height)).priorityLow();
    }]; 
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.formModel.dataSource.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEAITipsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHEAITipsCell forIndexPath:indexPath];
    cell.model = self.formModel.dataSource[indexPath.row];
      return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
////    return CGSizeMake(101, 121);
//}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 15, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
}

 


@end

